// 配置基础地址前缀
import { login } from "./auth";

const BASE_URL = "http://127.0.0.1:20427/ticket";
// const BASE_URL = "https://xuziheng.cn/ticket";
// 请求超时时间
const TIMEOUT = 1000;
export const TOKEN = 'token';
const request = function (uri, method, data, contentType, loading) {
    const url = BASE_URL + uri;
    // 封装认证
    var header = {
        Authorization: 'Bearer ' + uni.getStorageSync(TOKEN),
        contentType: contentType
    };
    // 开启loading
    if (loading) {
        uni.showLoading(loading)
    }
    return new Promise((resolve, reject) => {
        // 发起一个request请求
        uni.request({
            url, //请求url
            method: method, //请求方法
            header, //请求头
            TIMEOUT,
            data: data, //请求数据L
            success(res) {
                // 请求成功到达 后端
                if (res.statusCode === 200) {
                    // 如果是401 没有权限
                    if (res.data.code === 401) {
                        uni.showToast({
                            title: res.data.message,
                            icon: 'error',
                            complete: () => {
                                login();
                            }
                        })
                        reject(res.data.message)
                        return;
                    } else if (res.data.code === 500) {
                        uni.showToast({
                            title: res.data.message,
                            icon: 'error'
                        })
                        reject(res.data.message)
                        return;
                    }
                    // 判断是否存在业务异常
                    if (!res.data.success) {
                        uni.showToast({
                            title: res.data.message || '请求异常',
                            icon: 'error'
                        })
                        reject('请求异常！')
                        return;
                    }
                    // 请求成功 返回结果
                    resolve(res.data)
                } else {
                    uni.showToast({
                        title: res.data.message,
                        icon: 'error'
                    })
                    // 错误处理
                    reject(res.data.message)
                }
            },
            fail(err) {
                // 失败返回失败结果
                uni.showToast({
                    title: '请求失败',
                    icon: 'error'
                })
                console.error(err);
                reject(err)
            },
            complete() {
                // 完成 关闭loading
                if (loading) {
                    uni.hideLoading()
                }
            }
        })
    })
}
// /tickets/xx {x1:"xx"}  => /tickets/xx?x1=xx
request.get = function (config) {
    return request(config.url, 'GET', config.params || config.data, "application/x-www-form-urlencoded", config.loading || false);
}

// put
request.put = function (config) {
    return request(config.url, 'PUT', config.params || config.data, config.params ? "application/x-www-form-urlencoded" : "application/json", config.loading || false);
}

// delete
request.delete = function (config) {
    return request(config.url, 'DELETE', null, null, config.loading || false);
}

// post
request.post = function (config) {
    return request(config.url, 'POST',config.params || config.data, config.params ? "application/x-www-form-urlencoded" : "application/json", config.loading || false);
}


export default request;